﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XS.Tunnel.Cad.SteelBracing.Component.SteelShotcrete;
using XS.Tunnel.Cad.SteelBracing.Infrastructure.Extensions;

namespace XS.Tunnel.Cad.SteelBracing.Component
{
    public class SteelPlateComponent
    {
        public SteelPlateComponent(SteelPlateParameter parame)
        {
            Parame = parame;
        }


        public SteelPlateComponent(Line centerLine, SteelPlateParameter parame)
        {
            CenterLine = centerLine;
            Parame = parame;
        }

        /// <summary>
        /// 中心线
        /// </summary>
        public Line CenterLine { get; set; }


        public Point3d StartPoint { get; set; }
        public Point3d EndPoint { get; set; }
        /// <summary>
        /// 钢板参数
        /// </summary>
        public SteelPlateParameter Parame { get; set; }


        public Polyline Create( bool isLeft=true)
        {
            var polylins=new Polyline(); 

            double thickness= isLeft?-1*Parame.Thickness:Parame.Thickness;
             

            Vector3d curVector = CenterLine.GetFirstDerivative(CenterLine.StartPoint);
            Vector3d curPervector = curVector.GetPerpendicularVector();
            curPervector = curPervector.GetNormal();
            Point3d StartPoint = CenterLine.StartPoint.Add(curPervector * thickness);


            Vector3d curVector2 = CenterLine.GetFirstDerivative(CenterLine.EndPoint);
            Vector3d curPervector2 = curVector2.GetPerpendicularVector();
            curPervector2 = curPervector2.GetNormal();
            Point3d EndPoint = CenterLine.EndPoint.Add(curPervector2 * thickness);


            polylins.AddVertexAt(0, CenterLine.StartPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(1, StartPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(2, EndPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(3, CenterLine.EndPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(4, CenterLine.StartPoint.To2d(), 0, 0, 0);

            return polylins;
        }


        public Polyline CreateWallFoot(Line line)
        {
            var polylins = new Polyline();

            double thickness = Parame.Thickness*-1;


            Vector3d curVector = line.GetFirstDerivative(line.StartPoint);
            Vector3d curPervector = curVector.GetPerpendicularVector();
            curPervector = curPervector.GetNormal();
            Point3d startPoint = line.StartPoint.Add(curPervector * thickness);


            Vector3d curVector2 = line.GetFirstDerivative(line.EndPoint);
            Vector3d curPervector2 = curVector2.GetPerpendicularVector();
            curPervector2 = curPervector2.GetNormal();
            Point3d EndPoint = line.EndPoint.Add(curPervector2 * thickness);


            polylins.AddVertexAt(0, line.StartPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(1, startPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(2, EndPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(3, line.EndPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(4, line.StartPoint.To2d(), 0, 0, 0);

            return polylins;
        }

        /// <summary>
        /// 侧方钢板
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public Polyline CreateSidSteelPlate(Line line,bool isLeft = true)
        {
            var polylins = new Polyline();

            double thickness = isLeft ? -1 * Parame.Thickness:Parame.Thickness;



            Vector3d curVector = line.GetFirstDerivative(line.StartPoint);
            Vector3d curPervector = curVector.GetPerpendicularVector();
            curPervector = curPervector.GetNormal();
            Point3d startPoint = line.StartPoint.Add(curPervector * thickness);


            Vector3d curVector2 = line.GetFirstDerivative(line.EndPoint);
            Vector3d curPervector2 = curVector2.GetPerpendicularVector();
            curPervector2 = curPervector2.GetNormal();
            Point3d EndPoint = line.EndPoint.Add(curPervector2 * thickness);


            polylins.AddVertexAt(0, line.StartPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(1, startPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(2, EndPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(3, line.EndPoint.To2d(), 0, 0, 0);
            polylins.AddVertexAt(4, line.StartPoint.To2d(), 0, 0, 0);

            return polylins;
        }




    }
}
